<?php

namespace app\home\controller;


use think\facade\Request;

class Account extends Base {
    
    protected function initialize()
    {
        parent::initialize();
        $this->checkLogin();
    }
    
    /**
     * @desc 首页
     */
    public function index() {
        return view();
    }
    
    /**
     * @desc 个人信息
     */
    public function info() {
        $logic = model('Account', 'logic');
        $data = $logic->info($this->userId);
        $this->assign('info', $data);
        return view();
    }
    
    /**
     * @desc 修改密码
     */
    public function changePassword() {
        return view();
    }
    
    /**
     * @desc 修改密码
     */
    public function changePasswordAjax() {
        $params['password']     = Request::post('password');
        $params['newPassword1'] = Request::post('newPassword1');
        $params['newPassword2'] = Request::post('newPassword2');
        $params['userId']       = $this->userId;
    
        $logic = model('Account', 'logic');
        $data = [];
        try {
            $data = $logic->changePasswordAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 编辑邮箱
     */
    public function editEmail() {
        $action = input('action', 'edit');
        
        $this->assign('action', $action);
        return view();
    }
    
    /**
     * @desc 编辑邮件
     */
    public function editEmailAjax() {
        $params['password']  = Request::post('password');
        $params['email']     = Request::post('email');
        $params['emailCode'] = Request::post('emailCode');
        $params['userId']    = $this->userId;
    
        $logic = model('Account', 'logic');
        $data = [];
        try {
            $data = $logic->editEmailAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 获取邮件验证码
     */
    public function getEmailCode() {
        $params['email']  = Request::post('email');
        $params['userId'] = $this->userId;
        
        $logic = model('Account', 'logic');
        $data = [];
        try {
            $data = $logic->getEmailCode($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 编辑手机号
     */
    public function editPhone() {
        $action = input('action', 'edit');
    
        $this->assign('action', $action);
        return view();
    }
    
    /**
     * @desc 编辑邮件
     */
    public function editPhoneAjax() {
        $params['password']  = Request::post('password');
        $params['phone']     = Request::post('phone');
        $params['phoneCode'] = Request::post('phoneCode');
        $params['userId']    = $this->userId;
        
        $logic = model('Account', 'logic');
        $data = [];
        try {
            $data = $logic->editPhoneAjax($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 获取手机验证码
     */
    public function getPhoneCode() {
        $params['phone']  = Request::post('phone');
        $params['userId'] = $this->userId;
        
        $logic = model('Account', 'logic');
        $data = [];
        try {
            $data = $logic->getPhoneCode($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode);
    }
    
    /**
     * @desc 修改用户图像
     */
    public function editUserIcon() {
        $params['userId'] = $this->userId;
    
        $logic = model('Account', 'logic');
        $data = [];
        try {
            $data = $logic->editUserIcon($params);
        } catch (\Exception $e) {
            $logic->errorCode = EC_FAILURE;
            $logic->message = $e->getMessage();
        }
        return send_response($data, $logic->errorCode, $logic->message);
    }
}
